package com.trw.trp.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.trw.trp.bean.RoleBean;
import com.trw.trp.bean.UserBean;
import com.trw.trp.bean.UserSearchCriteriaBean;
import com.trw.trp.dao.DaoFactory;
import com.trw.trp.dao.UserDao;
import com.trw.trp.dao.UserDao.CHANGE_PWD_RESULT;
import com.trw.trp.msg.ResultMessage;
import com.trw.trp.util.TRPConstants;
import com.trw.trp.util.TRPUtil;

public class UserManagementAction extends AbstractAction {

	private long userId;

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public String execute() {
		return SUCCESS;
	}

	public void updateUser() throws IOException {
		UserBean curUser = (UserBean) TRPUtil.getFromSession(TRPConstants.CURRENT_USER);
		String name = getParamFromRequest("name");
		String email = getParamFromRequest("email");
		String division = getParamFromRequest("division");
		String department = getParamFromRequest("department");
		String position = getParamFromRequest("position");
		String tel = getParamFromRequest("tel");
		String mobile = getParamFromRequest("mobile");
		String isAdmin = getParamFromRequest("isAdmin");
		UserBean user = new UserBean();
		user.setEmail(email);
		user.setName(name);
		user.setDivision(division);
		user.setDepartment(department);
		user.setPosition(position);
		user.setTel(tel);
		user.setMobile(mobile);
		user.setUserId(userId);
		if ("Y".equalsIgnoreCase(isAdmin)) {
			List<RoleBean> roleList = new ArrayList<RoleBean>();
			user.setRoleList(roleList);
			RoleBean role = RoleBean.SUPER_ADMIN_ROLE;
			roleList.add(role);
		}
		UserDao uDao = DaoFactory.getUserDao();
		boolean success = uDao.updateUser(user);
		ResultMessage msg = new ResultMessage();
		msg.setState(success ? ResultMessage.SUCCESS : ResultMessage.FAILED);
		if (userId == curUser.getUserId()) {
			TRPUtil.putToSession(TRPConstants.CURRENT_USER, user);
		}
		writeJSON4Form(true, msg);
	}

	public void getUser() throws IOException {
		UserDao uDao = DaoFactory.getUserDao();
		UserBean user = uDao.getUser(userId);
		writeJSON(user);
	}

	public void getCurrentUser() throws IOException {
		UserBean curUser = (UserBean) TRPUtil.getFromSession(TRPConstants.CURRENT_USER);
		writeJSON(curUser);
	}

	public void getUsers() throws IOException {
		UserDao uDao = DaoFactory.getUserDao();
		UserSearchCriteriaBean criteria = new UserSearchCriteriaBean();
		String position = getParamFromRequest("position");
		if (!StringUtils.isEmpty(position)) {
			criteria.setPosition(position);
		}
		String department = getParamFromRequest("department");
		if (!StringUtils.isEmpty(department)) {
			criteria.setDepartment(department);
		}
		List<UserBean> userList = uDao.getUsers(criteria);
		writeJSON(userList);
	}

	public void deleteUser() throws IOException {
		ResultMessage ret = new ResultMessage();
		String userIdStr = getParamFromRequest("userId");
		String userName = getParamFromRequest("userName");
		String text = getParamFromRequest("text");
		UserBean user = (UserBean) TRPUtil.getFromSession(TRPConstants.CURRENT_USER);
		try {
			long userId = Long.parseLong(userIdStr);
			UserDao dao = DaoFactory.getUserDao();
			boolean result = dao.deleteUser(userId);
			if (result) {
				String msg = generateEmail(new Object[] { userName, user.getName(), text });
				ret.setMessage("user deleted." + msg);
			} else {
				ret.setState(ResultMessage.FAILED);
				ret.setMessage("delete user failed, please try again.");
			}
		} catch (Exception e) {
			ret.setState(ResultMessage.FAILED);
			ret.setMessage(e.getMessage());
		}
		writeJSON(ret);
	}

	public void changePassword() throws IOException {
		ResultMessage ret = new ResultMessage();
		UserBean user = (UserBean) TRPUtil.getFromSession(TRPConstants.CURRENT_USER);
		String oldPwd = getParamFromRequest("oldPwd");
		String newPwd = getParamFromRequest("newPwd");
		try {
			UserDao dao = DaoFactory.getUserDao();
			CHANGE_PWD_RESULT result = dao.changePassword(user.getUserId(), oldPwd, newPwd);
			if (result == CHANGE_PWD_RESULT.SUCCESS) {
				ret.setMessage("password changed.");
				generateEmail(new Object[] { newPwd });
			} else if (result == CHANGE_PWD_RESULT.OLD_PWD_WRONG) {
				ret.setState(ResultMessage.FAILED);
				ret.setMessage("current password is not correct.");
			} else {
				ret.setState(ResultMessage.FAILED);
				ret.setMessage("operation failed, please try again.");
			}
		} catch (Exception e) {
			ret.setState(ResultMessage.FAILED);
			ret.setMessage(e.getMessage());
		}
		writeJSON(ret);
	}

}
